import sys, os
import time
import threading
import wx

from GlobalMacro import *

from ToolPanel import ToolPanel
#######
# from MapEditPanel import MapEditPanel
#######


class MainFrame(wx.Frame):

    def __init__(self):
        wx.Frame.__init__(self, None, -1, APP_TITLE, style=wx.DEFAULT_FRAME_STYLE ^ wx.RESIZE_BORDER)
        # 默认style是下列项的组合：wx.MINIMIZE_BOX | wx.MAXIMIZE_BOX | wx.RESIZE_BORDER 
        # | wx.SYSTEM_MENU | wx.CAPTION | wx.CLOSE_BOX | wx.CLIP_CHILDREN 

        self.SetBackgroundColour("light blue")
        self.SetSize((WND_W, WND_H))
        self.Center()

        # 算核运维信息
        self.process_info = {}

        # 以下代码处理图标
        if hasattr(sys, "frozen") and getattr(sys, "frozen") == "windows_exe":
            exeName = win32api.GetModuleFileName(win32api.GetModuleHandle(None))
            icon = wx.Icon(exeName, wx.BITMAP_TYPE_ICO)
        else:
            icon = wx.Icon(APP_ICON, wx.BITMAP_TYPE_ICO)
        self.SetIcon(icon)

        # 创建分割窗
        self.sp = wx.SplitterWindow(self, style=wx.SP_NOBORDER)# 创建一个分割窗
        self.tool = ToolPanel(self.sp)


        ###################
        # 这个要改掉，改成 map_edit_panel
        self.list = wx.ListCtrl(self.sp, -1, style=wx.LC_ICON) #
        #
        ####################


        self.sp.SplitHorizontally(self.tool, self.list, sashPosition=INFOBAR_H)# 初始化分割窗
        self.Bind(wx.EVT_SPLITTER_SASH_POS_CHANGING, self.OnSashChanging, self.sp)

        # 初始化右键菜单
        self.popupmenu = wx.Menu()#创建一个菜单
        # 填充菜单
        #item = self.popupmenu.Append(-1, "new-core")
        #self.Bind(wx.EVT_MENU, self.On_new_core, item)
        #item = self.popupmenu.Append(-1, "core-info")
        #self.Bind(wx.EVT_MENU, self.On_core_info, item)
        #item = self.popupmenu.Append(-1, "kill-core")
        #self.Bind(wx.EVT_MENU, self.On_kill_core, item)
        #self.list.Bind(wx.EVT_CONTEXT_MENU, self.OnShowPopup)#绑定一个显示菜单事件
    
    def OnShowPopup(self, event):#弹出显示
        pos = event.GetPosition()
        pos = self.list.ScreenToClient(pos)
        self.list.PopupMenu(self.popupmenu, pos)

    def OnSashChanging(self, event):
        event.Veto()#禁止改变分割窗比例

    def OnCloseWindow(self, event):
        self.Destroy()

    
    # 守护线程，定时更新icon
    def ThreadRefreshIcon(self, exit_event):
        pass


class MainApp(wx.App):
    def OnInit(self):
        # 显示闪屏窗口
        bmp = wx.Image("splash.jpg").ConvertToBitmap()
        wx.adv.SplashScreen(bmp,
            wx.adv.SPLASH_CENTRE_ON_SCREEN | wx.adv.SPLASH_TIMEOUT,
            3000, None, -1)
        self.SetAppName(APP_TITLE)
        self.Frame = MainFrame()
        self.Frame.Show()

        # 启动守护线程，用于更新各个 list_icon 的图标
        self.exit_event = threading.Event()
        self.daemon_thread = threading.Thread (target=self.Frame.ThreadRefreshIcon,
                                               name="thread_refresh_icon",
                                               args=(self.exit_event,))
        self.daemon_thread.daemon = True
        self.daemon_thread.start()
        return True


if __name__ == "__main__":
    #app = MainApp(redirect=True, filename="errlog.txt")
    app = MainApp()
    app.MainLoop()
    app.Frame.CloseAllCore()
    app.redis_deamon.Exit()
    app.exit_event.set()
    app.daemon_thread.join()
    print ("BYE-BYE MainWindow")
